<template>
  <div class="newAdress">

    <cc-addressSet :colors="colors" :addressList="addressList" @chooseClick="chooseClick"
            @editClick="editClick" @delAddress="delAddress"></cc-addressSet> 


            <view class="save">
            <view class="btn" :style="'background:' + colors" @click="addAddress">添加收货地址</view>
        </view>
  </div>
</template>

<script setup>
import { onShow } from "@dcloudio/uni-app";
import { ref } from "vue";
import {getAdress,defaultAddr,deleteAddr} from '../https/api/adress';

let colors = ref('#FF5951')


onShow(()=>{
  getAdressData({})
})

let getAdressData = async(data) => {
  let res = await getAdress(data)
  if(res){
    addressList.value = res.data.map(v=>{
      return {
        ...v,
        name:v.receiver,
        phone:v.mobile,
        address:`${v.province}${v.city}${v.area}`,
        moreAddres:v.addr,
        isdefult:v.commonAddr,
        isCheck:false,
      }
    })
    
  }
}

let addAddress = () => {
  uni.navigateTo({
    url:'/pages/addAdress'
  })
}


let delAddress = (item,index) => {
  uni.showModal({
					title: '提示',
					content: '确认要删除该地址吗?',
					confirmText: '删除',
				
					success:async (res) => {
						if (res.confirm) {
							uni.showToast({
								title: '删除成功~',
								icon: 'none'
							})
              await deleteAddr(item.addrId)
              getAdressData({})
						}
					}
				})

        
}

let editClick = (item) => {
uni.setStorageSync('adress',JSON.stringify(item))


uni.navigateTo({
  url:'/pages/editAdress'
})
}
let chooseClick =async (item) => {
  addressList.value.forEach(v=>{
    v.isdefult = 0
  })
  item.isdefult = 1
  
 await defaultAddr(item.addrId,{addrId:item.addrId})
}
let addressList= ref([{
                    name: '小明',
                    phone: '12345678915',
                    address: '广东省佛山市南海区',
                    moreAddres: '桂城街道',
                    isdefult: 1,
                    id: 1
                }, {
                    name: '小红',
                    phone: '12345678915',
                    address: '广东省广州市海珠区',
                    moreAddres: '昌岗路15号',
                    isdefult: 0,
                    id: 2
                }])
</script>

<style lang="scss" scoped>

.save {
        position: fixed;
        bottom: 0;
        width: 100%;
        height: 120upx;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .save view {
        display: flex;
    }

    .save .btn {
        box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
        width: 70%;
        height: 80upx;
        border-radius: 80upx;
        background-color: #f23a3a;
        color: #fff;
        justify-content: center;
        align-items: center;
        font-size: 30upx;
        margin-bottom: 60px;
    }
</style>